
package com.akupilih.app.helper;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.akupilih.app.ACRAPostSender;
import com.akupilih.app.entity.E_Berita;
import com.akupilih.app.entity.E_Chart;
import com.akupilih.app.model.DBClass;

public class JSONHelper {

    /**
     * SEMUA PARSINGAN LOGIC TARUH SINI
     * 
     * @param JsonArray
     * @return list
     */

    public static List<E_Chart> getTimeChart(JSONArray arr) {
        List<E_Chart> list = new ArrayList<E_Chart>();

        try {
            for (int i = 0; i < arr.length(); i++) {
                E_Chart trend = new E_Chart();
                JSONObject obj = arr.getJSONObject(i);
                trend.setTrend_tgl(obj.getString("tgl"));
                JSONArray arraybuzz = obj.getJSONArray("buzz");
                for (int j = 0; j < 2; j++) {
                    trend.setTrend_buzz1(arraybuzz.getInt(0));
                    trend.setTrend_buzz2(arraybuzz.getInt(1));
                }
                list.add(trend);
            }
        } catch (JSONException e) {
            ACRAPostSender.TRACE("JSONHelper", "getChart : " + e.getMessage());
        }

        return list;

    }

    public static List<E_Chart> getSentimentChart(JSONArray chart) {
        List<E_Chart> list = new ArrayList<E_Chart>();
        JSONObject objChart;
        try {
            objChart = chart.getJSONObject(1);
            JSONArray data = objChart.getJSONArray("data");

            for (int i = 0; i < data.length(); i++) {
                E_Chart Echart = new E_Chart();
                JSONObject job = data.getJSONObject(i);
                JSONArray value = job.getJSONArray("value");
                Echart.setCandidate(value.getString(0));
                Echart.setSentiment(value.getInt(1));
                Echart.setPeopletalk(value.getInt(4));
                list.add(Echart);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            ACRAPostSender.TRACE("JSONHelper", "getChart : " + e.getMessage());
        }

        return list;
    }

    public static List<E_Chart> getTotalTalk(JSONArray chart) {
        List<E_Chart> list = new ArrayList<E_Chart>();
        JSONObject objChart;
        try {
            objChart = chart.getJSONObject(2);
            JSONObject data = objChart.getJSONObject("data");

            for (int i = 1; i <= data.length(); i++) {
                E_Chart Echart = new E_Chart();
                JSONObject job = data.getJSONObject(i + "");
                Echart.setCandidate(job.getString("name"));
                Echart.setTotalTalk(job.getInt("BZ"));
                list.add(Echart);
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            ACRAPostSender.TRACE("JSONHelper", "getChart : " + e.getMessage());
        }

        return list;
    }

    public static List<E_Chart> getBarChart(JSONArray data) {
        List<E_Chart> list = new ArrayList<E_Chart>();

        try {
            for (int i = 0; i < data.length(); i++) {
                JSONArray arr = data.getJSONArray(i);
                E_Chart Echart = new E_Chart();
                Echart.setSosmed(arr.getString(0));
                Echart.setSosmed_val1(arr.getInt(1));
                Echart.setSosmed_val2(arr.getInt(2));
                list.add(Echart);

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            ACRAPostSender.TRACE("JSONHelper", "getChart : " + e.getMessage());
        }

        return list;
    }

    public static List<E_Berita> getBerita(JSONArray root, Context contex) {
        List<E_Berita> list = new ArrayList<E_Berita>();
        DBClass db = new DBClass(contex);
        try {
            for (int i = 0; i < root.length(); i++) {
                JSONObject job = root.getJSONObject(i);
                E_Berita Eberita = new E_Berita();
                Eberita.setId(job.getInt("id"));
                Eberita.setUrl(job.getString("url"));
                Eberita.setTitle(job.getString("title"));
                Eberita.setContent(job.getString("content"));
                Eberita.setExcerpt(job.getString("excerpt"));
                Eberita.setDate(job.getString("date"));

                JSONObject author = job.getJSONObject("author");
                Eberita.setAuthor(author.getString("name"));
                list.add(Eberita);
                db.addBerita(Eberita);

            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            ACRAPostSender.TRACE("JSONHelper", "Berita : " + e.getMessage());
        }
        return list;

    }

}
